/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.ProdukDAOInterface;
import com.wii.elpro.dao.hb.ProdukDAOImpl;
import com.wii.elpro.entity.Produk;
import com.wii.elpro.entity.ProdukMaterial;
import com.wii.elpro.entity.User;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class ProductSaveMaterialAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        ActionForward af = null;
        ProdukDAOInterface dao = new ProdukDAOImpl();       
        ActionMessages messages = new ActionMessages();
        String id;
        if(sess.getAttribute("idProduct")!=null) {
            id = (String) sess.getAttribute("idProduct");
        } else {
            id = 0+"";
        }
        try {
            if(sess.getAttribute("idProduct")!=null && sess.getAttribute("listMaterial")!=null) {
                List<ProdukMaterial> materials = (List<ProdukMaterial>) sess.getAttribute("listMaterial");
                Produk p = dao.getProduk(Long.parseLong((String) sess.getAttribute("idProduct")));
                p.getProdukMaterials().addAll(materials);
////                PENGURANGAN STOCK
//                for(ProdukMaterial pm: materials) {
////                    STOCK LI
//                    pm.getMaterial().setStockLI(pm.getMaterial().getStockLI()-pm.getJumlah());
////                    STOCK LE
//                    pm.getMaterial().setStockLE(pm.getMaterial().getStockLE()-pm.getJumlah());
//                    dao.update(pm.getMaterial());
//                }
                dao.update(p);
                sess.removeAttribute("listMaterial");
                messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully saved."));
                saveMessages(sess, messages);
            } else {
                messages.add("message", new ActionMessage("customMessage", "There is no material(s) requested to save."));
                saveMessages(sess, messages);
            }
            af = new ActionForward(mapping.findForward("success").getPath()+"?id="+id, true);
        } catch (Exception ex ) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to save your data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("fail").getPath()+"?id="+id, true);
        }
        return af;
    }
}
